package com.example.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.contant.MessageConstant;
import com.example.entity.PageResult;
import com.example.entity.QueryPageBean;
import com.example.entity.Result;
import com.example.pojo.User;
import com.example.service.UserByAdminService;
import com.example.utils.QiniuUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RequestMapping("/user")
@RestController
public class UserController {
    @Reference
    private UserByAdminService userByAdminService;
    @GetMapping("/getUserName")
    public Result getUserName(){
        org.springframework.security.core.userdetails.User principal =(org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal==null)
        {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,principal.getUsername());

    }
    @RequestMapping ("upload")
    public Result upload(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();//原始名字
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i);
        String prefix= UUID.randomUUID().toString();
        String filename=prefix+suffix;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filename);
    }
    @PostMapping("/findpage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userByAdminService.findpage(queryPageBean);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody User user){
        System.out.println(user);
        try{
            userByAdminService.add(user);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }
    @GetMapping("/delete")
    public Result delete(Integer id){

        try{
            userByAdminService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }
    @GetMapping("/findById")
    public Result findById(Integer id){
        try{
            User user=userByAdminService.findById(id);
            return  new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody User user){
        try{
            userByAdminService.edit(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);

    }
}
